<?php
/**
 * @property string $content
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class HTMLBlock extends Block{
    public $content;

    const TYPE = 'html';

    public function rules() {
        return CMap::mergeArray(parent::rules(), array(
            array('content', 'required'),
            array('content', 'safe')
        ));
    }

    /**
     * set default scopes
     * @return type
     */
    public function defaultScope() {
        return array(
            'condition' => "type = 'html'"
        );
    }

    /**
     * set default block type after validate
     */
    public function beforeValidate() {
        parent::beforeValidate();

        $this->type = self::TYPE;

        return true;
    }

    public function beforeSave() {
        if(parent::beforeSave()){
            $this->type = self::TYPE;
            $this->params = $this->content;
            return true;
        }
    }

    public function afterFind() {
        parent::afterFind();

        $this->content = $this->params;
    }

    public function getEditUrl() {
        return url('/BlockManager/html/update', array('id' => $this->block_id));
    }
}